﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace ĐỒ_ÁN_QLDA
{
    public partial class frmXemdiem : Form
    {
        String conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection conn;
        SqlCommand cmd, cmd_them, cmd_sua, cmd_xoa;
        SqlDataAdapter da;
        DataSet ds,ds_cmb;
        String back = "no";
        public frmXemdiem(String back)
        {
            InitializeComponent();
            this.back = back;
        }

        public frmXemdiem()
        {
            InitializeComponent();
        }

        private void frmXemdiem_Load(object sender, EventArgs e)
        {
            btnSua.Enabled = false;
            btnXoa.Enabled = false;

            ds = new DataSet();
            conn = new SqlConnection(conStr);
            conn.Open();
            cmd = new SqlCommand("Select * from Diem", conn);
            da = new SqlDataAdapter(cmd);
            da.Fill(ds, "Diem");
            dataGridView1.DataSource = ds.Tables[0];
            conn.Close();

            //load combobox
            conn = new SqlConnection(conStr);
            conn.Open();
            cmd = new SqlCommand("Select Namhoc, Lophoc, Hocky, Monhoc from Diem", conn);
            da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            ds_cmb = new DataSet();
            da.Fill(ds_cmb, "Năm học, Lớp học, Học kỳ, Môn học");

            cmbNamhoc.DataSource = ds_cmb.Tables[0];
            cmbNamhoc.ValueMember = ds_cmb.Tables[0].Columns[0].ToString();
            cmbNamhoc.DisplayMember = ds_cmb.Tables[0].Columns[0].ToString();
            cmbLop.DataSource = ds_cmb.Tables[0];
            cmbLop.ValueMember = ds_cmb.Tables[0].Columns[0].ToString();
            cmbLop.DisplayMember = ds_cmb.Tables[0].Columns[0].ToString();
            cmbHocky.DataSource = ds_cmb.Tables[0];
            cmbHocky.ValueMember = ds_cmb.Tables[0].Columns[0].ToString();
            cmbHocky.DisplayMember = ds_cmb.Tables[0].Columns[0].ToString();
            cmbMonhoc.DataSource = ds_cmb.Tables[0];
            cmbMonhoc.ValueMember = ds_cmb.Tables[0].Columns[0].ToString();
            cmbMonhoc.DisplayMember = ds_cmb.Tables[0].Columns[0].ToString();
            da.Dispose();
            conn.Close();
        }

        private void bntThem_Click(object sender, EventArgs e)
        {
            if (txtHocsinh.Text == "")
            {
                MessageBox.Show("Nhập tên học sinh!");
            }
            else
            {
                conn = new SqlConnection(conStr);
                conn.Open();
                cmd_them = new SqlCommand("insert into Diem(Hocsinh,Namhoc,Lophoc,Hocky,Monhoc) values('" + txtHocsinh.Text + "','" + cmbNamhoc.SelectedValue + "','" + cmbLop.SelectedValue + "','" + cmbHocky.SelectedValue + "','" + cmbMonhoc.SelectedValue + "')", conn);
                cmd_them.ExecuteNonQuery();
                conn.Close();
                this.frmXemdiem_Load(sender, e);
                MessageBox.Show("Creat new successfully!");
            }
        }
          String id_tmp;
        private void bntSua_Click(object sender, EventArgs e)
        {
            conn = new SqlConnection(conStr);
            conn.Open();
            cmd_sua = new SqlCommand("update Diem set Hocsinh = '" + txtHocsinh.Text + "',Namhoc = '" + cmbNamhoc.SelectedValue + "',Lophoc = '" + cmbLop.SelectedValue + "',Hocky = '" +  cmbHocky.SelectedValue + "',Monhoc = '" + cmbMonhoc.SelectedValue + "' where id = " + id_tmp, conn);
            cmd_sua.ExecuteNonQuery();
            conn.Close();
            this.frmXemdiem_Load(sender, e);
            MessageBox.Show("Update successfully!");
        }

        private void bntXoa_Click(object sender, EventArgs e)
        {
             if (MessageBox.Show("Ban muon xoa khong?", "Notice", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                conn = new SqlConnection(conStr);
                conn.Open();
                cmd_xoa = new SqlCommand("Delete from Diem where id = '" + id_tmp + "'", conn);
                cmd_xoa.ExecuteNonQuery();
                conn.Close();
                this.frmXemdiem_Load(sender, e);
                MessageBox.Show("Delete successfully!");
            }
        }

        private void bntThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
             try
            {
                btnSua.Enabled = true;
                btnXoa.Enabled = true;

                DataGridViewRow row = new DataGridViewRow();
                row = dataGridView1.Rows[e.RowIndex];
                id_tmp = row.Cells[0].Value.ToString();
                txtHocsinh.Text = row.Cells[1].Value.ToString();
                cmbNamhoc.SelectedValue = row.Cells[2].Value.ToString();
                cmbLop.SelectedValue = row.Cells[3].Value.ToString();
                cmbHocky.SelectedValue = row.Cells[4].Value.ToString();
                cmbMonhoc.SelectedValue = row.Cells[5].Value.ToString();
            }
            catch { }
        }
        }
    }
